<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="Entity.Courtier"%>
<%@page import="Entity.Expert"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Extranet</title>
<link type="text/css" rel="stylesheet" href="form.css" />
        <style type="text/css">
		  body {
			padding-top: 160px;
			padding-bottom: 40px;
		  }
	</style>
    </head>


    <body>
        <%@page import="Session.ExpertSessionLocal"%>
        <%@page import="Session.CourtierSessionLocal"%>
        <%@page import="javax.naming.InitialContext" %>
        <%@page import="javax.ejb.EJBException" %>
        <%String user = request.getParameter("user");
            String pseudo = request.getParameter("pseudo");
            String pwd = request.getParameter("password");
            if (pseudo == null || pseudo.equals("") || pwd == null || pwd.equals("")) {
                if (request.getMethod().equals("POST")) { // Form is partially filled-in
        %>
        <p>Champs vides</p>
        <%
            }
        } else if (user.equals("expert")) {
            try {
                InitialContext ic = new InitialContext();
                ExpertSessionLocal rsl = (ExpertSessionLocal) ic.lookup("java:comp/env/ExpertSessionLocal");
                if (rsl.connexion(pseudo, pwd)) {
                    Expert c = rsl.getExpert(pseudo);
                    String id = ""+c.getIdexpert();
                    session.setAttribute("id", id);
                    session.setAttribute("user",user);
                    response.sendRedirect("ActionExpert.jsp");
                    
                }
            } catch (EJBException e) {
        %>
        <p> error <%=e.getLocalizedMessage()%></p>
        <%
            }
        } else if (user.equals("courtier")){
            try {
                InitialContext ic = new InitialContext();
                CourtierSessionLocal cs = (CourtierSessionLocal) ic.lookup("java:comp/env/CourtierSessionLocal");
                if (cs.connexion(pseudo, pwd)) {
                    Courtier c = cs.getCourtier(pseudo);
                    String id = ""+c.getIdcourtier();
                    session.setAttribute("id", id);
                    session.setAttribute("user",user);
                    
                    response.sendRedirect("ActionCourtier.jsp");
                }
            } catch (EJBException e) {
        %>
        <p> error <%=e.getLocalizedMessage()%></p>
        <%
                }

            }

        %>
        
        
        <div class="navbar navbar-inverse navbar-fixed-top">
         <div class="navbar-inner">
            <div class="container">
                <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="brand" id="logo" href="#">Assur'Tout</a>
            </div>
         </div>
      </div>

     <div class="container">
           <div class="hero-unit" align=center>
                <form  method="post" action="">
                    <h1>Identification</h1>   <br>      
                        <table>
                            <tr>
                                <td>Login</td>
                                <td><input type="text" name="pseudo" size="25" value=""></td>
                            </tr>
                            <tr>
                                <td>Password</td>
                                <td><input type="password" name="password" size="25" value=""></td>
                            </tr>
                            <tr>                
                        <td>Utilisateur</td>
                        <td>Courtier<input type="radio" name="user" value="courtier" /></td>
                        <td>Expert<input type="radio" name="user" value="expert" checked/></td>
                    </tr>

                </table>
                <input type="submit" class="btn btn-primary btn-large" name="submit" value="Submit">
            </form>
                </div>
             </div>
	 <footer> 
             <div align=center> Guillaume DI LIBERATORE - Mylène DOITEAU - Adrien HENRIET - Nathan KRUCK - Kilian POMIER - Gautier ROLS</div>
         </footer>
  </body>
</html>
